﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace HealthExamination.HardwareDrivers.Commons
{
   public static class HelperINI
    {
        //声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //[DllImport("kernel32")]
        //private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #region 读写ini文件
        ///  <summary>
        ///   写INI文件
        ///  </summary>
        ///  <param  name="Section">Section</param>
        ///  <param  name="Key">Key</param>
        ///  <param  name="value">value</param>
        public static void WriteIniValue(string strFile, string Section, string Key, string value)
        {
            WritePrivateProfileString(Section, Key, value, strFile);
        }

        ///  <summary>
        ///    读取INI文件指定部分
        ///  </summary>
        ///  <param  name="Section">Section</param>
        ///  <param  name="Key">Key</param>
        ///  <returns>String</returns>  
        public static string ReadIniValue(string strFile, string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(3072);
            long i = GetPrivateProfileString(Section, Key, "", temp, 3072, strFile);
            return temp.ToString();

        }
        #endregion
    }
}
